package com.sc.nft.admin.rpc;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.FilterIncomeListDao;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.dto.MerchantViewBillDTO;
import com.sc.nft.entity.vo.AdminPageDaoMarketValueAccountVO;
import com.sc.nft.entity.vo.ExpenditureRecordVO;
import com.sc.nft.entity.vo.MerchantViewBillVO;
import com.sc.nft.entity.vo.PointsApplyExchangeRecordsPageVO;
import com.sc.nft.entity.vo.ecology.adminDaoPhoneVO;
import com.sc.nft.enums.PointsExchangeCollectionStatusEnum;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.*;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/marketValueAccount")
public class MarketValueAccountRpc {

    private final FilterIncomeListDao filterIncomeListDao;
    private final UserEcologyPointsService userEcologyPointsService;
    private final Redisson redisson;
    private final ExpenditureRecordService expenditureRecordService;
    private final DaoEcologyPointsApplyExchangeRecordsService daoEcologyPointsApplyExchangeRecordsService;

    private final UserCreatorInfoService userCreatorInfoService;
    private final DaoEcologyPointsService daoEcologyPointsService;
    @ApiOperation(value = "分页条件获取", notes = "分页条件获取", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<AdminPageDaoMarketValueAccountVO> getPage(@RequestBody PageDaoMarketValueAccountRequest request) {
        return PageResult.success(filterIncomeListDao.getPage(request.getDaoId(),request.getUserTel(),request.getPageNo(),request.getPageSize()));
    }

    @ApiOperation(value = "根据DAO id获取", notes = "根据DAO id获取", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getListByDaoId", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<Long>> getListByDaoId(@RequestBody IdRequest request) {
        return Result.success(filterIncomeListDao.getListByDaoId(request.getId()));
    }

    @ApiOperation(value = "企业账号转入到市值账号", notes = "企业账号转入到市值账号", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/transferMarketValueAccount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<Long>> transferMarketValueAccount(@RequestBody FeignTransferMarketValueAccountRequest request) {

        RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_MARKET_VALUE_ACCOUNT_LOCK.getKey() + request.getDaoId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                userEcologyPointsService.transferMarketValueAccount(request.getUserId(),request.getDaoId(),request.getTransferAmount());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return Result.fail("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }


        return Result.success();
    }


    @ApiOperation(value = "燃料兑换nft", notes = "燃料兑换nft", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pointApplyExchange", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result pointApplyExchange(@RequestBody FeignPointApplyExchangeRequest request) {

        RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_MARKET_VALUE_ACCOUNT_LOCK.getKey() + request.getDaoId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
//                userEcologyPointsService.pointApplyExchange(request.getToUserId(),request.getDaoId(),request.getApplyNum(),request.getCollectionId(),request.getNeedPoints());
                daoEcologyPointsService.pointApplyExchange(request.getToUserId(),request.getDaoId(),request.getApplyNum(),request.getCollectionId(),request.getNeedPoints(),request.getPointsId(),request.getConsumeType());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return Result.fail("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }


        return Result.success();
    }



    @ApiOperation(value = "燃料兑换审核列表", notes = "燃料兑换审核列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pointApplyPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<PointsApplyExchangeRecordsPageVO> pointApplyPage(@Valid @RequestBody PointsApplyExchangeRecordsPageReq pointsApplyExchangeRecordsPageReq) {
        return PageResult.success(daoEcologyPointsApplyExchangeRecordsService.page(pointsApplyExchangeRecordsPageReq.getDaoId(),pointsApplyExchangeRecordsPageReq.getApplyStatus(),
                pointsApplyExchangeRecordsPageReq.getCollectionId(),pointsApplyExchangeRecordsPageReq.getStartTime(),pointsApplyExchangeRecordsPageReq.getEndTime(),pointsApplyExchangeRecordsPageReq.toMp()
        ,pointsApplyExchangeRecordsPageReq.getConsumeType()));
    }




    @ApiOperation(value = "撤销兑换申请", notes = "燃料兑换nft", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pointApplyRevoked", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result pointApplyRevoked(@RequestBody FeignPointApplyExchangeRevokedRequest request) {

        RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_MARKET_VALUE_ACCOUNT_LOCK.getKey() + request.getDaoId());
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (res) {
                daoEcologyPointsApplyExchangeRecordsService.audit(request.getId(),"", PointsExchangeCollectionStatusEnum.REVOKED);
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return Result.fail("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }

        return Result.success();
    }

    /**
     * 根据daoId品牌手机号
     * @param adminPhoneRequest
     */
    @PostMapping(value = "/adminPhoneById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据daoId查手机号", notes = "根据daoId查手机号", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public adminDaoPhoneVO adminPhoneById(AdminPhoneRequest adminPhoneRequest){
        return userCreatorInfoService.adminPhoneById(adminPhoneRequest.getId());
    }
}
